<?php
/**
 * 
 * 目录导航
 * @author LeoNing
 *
 */
class CategoryDataProvider implements IDataProvider {
	/*
	 * (non-PHPdoc) @see IDataProvider::provide()
	 */
	public function provide($params) {
		global $_PAGE;
		extract ( $params );
		$where = array ('CATE.up_cid' => 0, 'CATE.display' => 1, 'CATE.page_id > ' => 0, 'WP.status' => 'published' );
		$_cateM = new WebPageCategory ();
		$_cateM->alias ( 'CATE' );
		if (isset ( $parent ) && ! empty ( $parent )) { //读取子分类
			$upid = intval ( $parent );
			$where ['CATE.up_cid'] = $upid;
		}
		if (isset ( $type ) && ! empty ( $type )) { //类型
			$types = apply_filter ( 'get_category_types', array () );
			$tname = explode ( ",", $type );
			$tids = array ();
			foreach ( $tname as $tn ) {
				$tid = array_search ( $tn, $tname );
				if($tid !== false){
					$tids [] = $tid;
				}
			}
			if(!empty($tids)){
				$where ['CATE.type IN'] = $tids;
			}
		}
		$_cateM->join ( "web_page AS WP", "CATE.page_id = WP.page_id" );
		$_cateM->sortAsc ( 'sort' );
		$items = $_cateM->where ( $where )->retrieve ( 'CATE.cid as id,CATE.title as name,CATE.page_id,WP.title,WP.url,CATE.bind,CATE.home as domain_home' );
		$menus = array ();
		if ($items) {
			foreach ( $items as $item ) {
				$item ['subactived'] = $this->isActived ( $item ['page_id'], $_PAGE ['subcrumbs'] );
				if ($item ['page_id'] == $_PAGE ['page_id']) {
					$item ['actived'] = true;
				} else {
					$item ['actived'] = $this->isActived ( $item ['page_id'], $_PAGE ['crumbs'] );
				}
				$menus [] = $item;
			}
		}
		return new CtsDataSet ( $menus );
	}
	protected function isActived($page_id, $crumbs) {
		if (! empty ( $crumbs )) {
			foreach ( $crumbs as $c ) {
				if ($c ['page_id'] == $page_id && $c ['page_id']) {
					return true;
				}
			}
		}
		return false;
	}
}